import Vue from 'vue'
import VueRouter from 'vue-router'
import request from "../utils/request";
import store from "../store";
Vue.use(VueRouter)

const routes = [
  {
    //path"/" 和 component，就是一组映射页面的展示
    path: '/',
    name: 'Home',
    component: () => import('../views/Home'),
    redirect:"/home",
    children:[
        {
            path: '/sys/user',
            name: 'User',
            component: () => import("../views/sys/User"),
        },
        {
            path: '/index',
            name: 'Index',
            component: () => import('../views/Index')
        },
        {
            path: '/sys/role',
            name: 'Role',
            component: () => import('../views/sys/Role')
        },
        {
            path: '/sys/menu',
            name: 'Menu',
            component: () => import('../views/sys/Menu')
        },
        {
            path: '/book',
            name: 'Book',
            component: () => import('../views/Book')
        },
        {
            path: '/news',
            name: 'News',
            component: () => import('../views/News')
        }
        ,{
            path: '/person',
            name: 'Person',
            component: () => import('../views/Person')
        },
    ]
  },
    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/Login')
    },

    // {
    //     path: '/index',
    //     name: 'Index',
    //     component: () => import('../views/Index')
    // },
    {
        path: '/register',
        name: 'Register',
        component: () => import('../views/Register')
    }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//动态绑定路由，判断有没有路由
router.beforeEach((to, from, next) => {

    // request.get("/user/sys/menu/nav",{
    //         headers:{
    //             Authorization: localStorage.getItem("accessToken")
    //         }
    //     }).then(res=>{
    //
    //         console.log("nav:"+JSON.stringify(res.data))
    //             // .nav) + "\n" + "authorities:"+JSON.stringify(res.data.authorities));
    //         //    拿到menuList
    //         store.commit("setMenuList",JSON.stringify(res.data.nav))
    //         store.commit("setPermList",JSON.stringify(res.data.authorities))
    //
    //         alert("是这里 res:" + "\n" + "nav:" + JSON.stringify(res.data.nav) + "\n" + "authorities:" + JSON.stringify(res.data.authorities))
    //     })
    //     next()

    if (to.path ==='/login')return next()
    //获取token
    const token = window.sessionStorage.getItem("accessToken")
    console.log('token:'+token)
    if (!token) return next('/login')
    next()
})

export default router
